<template>
  <div class="pd-20">
    <el-input v-model="drugName" clearable placeholder="药品名称" class="w-200"></el-input>
    <el-button type="primary"
               @click="handleAddDrug">新 增
    </el-button>
    <element-table class="mt-20"
                   :columns="columns"
                   :data="tableData"
                   v-loading="loading"
                   pager
                   :pageTotal="pageTotal"
                   :pageIndex="pageIndex"
                   :pageSize="pageSize"
                   @handleChangePage="handleChangePage">

      <template v-slot:option="{row,index}">
        <el-button type="danger"
                   @click="handleDelDrug(row)">删除
        </el-button>
      </template>
    </element-table>

  </div>
</template>

<script>
import ElementTable from '@/base/ElementTable'

export default {
  name: "index",
  data() {
    return {
      drugName: '',
      loading: false,
      pageTotal: 0,
      pageSize: 20,
      pageIndex: 1,
      tableData: [],
      columns: [
        {
          label: '药品名称',
          prop: 'name',
          align: "center",
        }, {
          label: '操作',
          prop: 'option',
          align: 'center',
          isSlot: true,
        }
      ],

    }
  },
  created() {
    this.getList()
  },
  components: {
    'element-table': ElementTable
  },
  methods: {
    getList() {
      this.post(this.api.slowDisease.getDellDrugList).then((res) => {
        if (res.data.code === '200') {
          this.tableData = res.data.object
        }
      })
    },
    saveDellDrug() {
      this.post(this.api.slowDisease.saveDellDrug,{
        name:this.drugName
      }).then((res) => {
        if (res.data.code === '200') {
          this.$message.success('添加成功')
          this.drugName=''
          this.getList()
        }
      })
    },
    handleAddDrug() {
      if (this.drugName) {
        this.saveDellDrug()
      } else {
        this.$message.error('请输入药品名称')
      }

    },
    handleDelDrug(row) {
      this.$confirm('确认删除分类?', '提示', {
        type: 'warning'
      }).then(() => {
        this.post(this.api.slowDisease.deleteDellDrug,{
          id:row.id
        }).then((res) => {
          if (res.data.code === '200') {
            this.$message.success('删除成功')
            this.getList()
          }
        })
      })
    },

    // 分页
    handleChangePage(page) {
      this.pageIndex = page;
      this.getList();
    },
  }
}
</script>

<style scoped>

</style>
